<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年度" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年度" style="width: 120px;" clearable>
          <el-option
            v-for="dict in years"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属机构" prop="institutionId">
        <el-select v-model="queryParams.institutionId" placeholder="请选择机构" style="width: 140px;" clearable>
          <el-option
            v-for="dict in institutions"
            :key="dict.id"
            :label="dict.institutionname"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选择团队" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择团队" style="width: 120px;"clearable>
          <el-option
            v-for="dict in teams"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['teamper:teamper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teamper:teamper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teamper:teamper:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['teamper:teamper:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="年度" align="center" prop="year">
        <template #default="scope">
          <dict-tag :options="years" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="机构id" align="center" prop="institutionId">
        <template #default="scope">
          <dict-tag :options="years" :value="scope.row.institutionId"/>
        </template>
      </el-table-column> -->
      <el-table-column label="团队编号" align="center" prop="teamId">
        <template #default="scope">
          <!-- <dict-tag :options="teams" :value="scope.row.id"/> -->
          <span v-for="item in teams">
            <span v-if="item.id == scope.row.teamId">
              {{ item.id }}
            </span>
           </span>
        </template>
      </el-table-column>
      <el-table-column label="团队名称" align="center" prop="teamId">
        <template #default="scope">
          <!-- <dict-tag :options="teams" :value="scope.row.name"/> -->
           <span v-for="item in teams">
            <span v-if="item.id == scope.row.teamId">
              {{ item.name }}
            </span>
           </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="全年目标" align="center" prop="targets" />
        -->
      <el-table-column label="全年目标" align="center" prop="alltarget"/>
      <el-table-column label="第一季度" align="center" prop="onequarter" />
      <el-table-column label="1月" align="center" prop="january" />
      <el-table-column label="2月" align="center" prop="february" />
      <el-table-column label="3月" align="center" prop="march" />
      <el-table-column label="第二季度" align="center" prop="twoquarter" />
      <el-table-column label="4月" align="center" prop="april" />
      <el-table-column label="5月" align="center" prop="may" />
      <el-table-column label="6月" align="center" prop="june" />
      <el-table-column label="第三季度" align="center" prop="threequarter" />
      <el-table-column label="7月" align="center" prop="july" />
      <el-table-column label="8月" align="center" prop="august" />
      <el-table-column label="9月" align="center" prop="september" />
      <el-table-column label="第四季度" align="center" prop="fourquarter" />
      <el-table-column label="10月" align="center" prop="october" />
      <el-table-column label="11月" align="center" prop="november" />
      <el-table-column label="12月" align="center" prop="december" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="130px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['teamper:teamper:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['teamper:teamper:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改团队业绩目标对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="teamperRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年度" prop="year">
          <el-select v-model="form.year" placeholder="请选择年度">
            <el-option
              v-for="dict in years"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机构名称" prop="institutionId">
          <el-select v-model="form.institutionId" placeholder="请选择机构">
            <el-option
              v-for="dict in institutions"
              :key="dict.id"
              :label="dict.institutionname"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团队名称" prop="teamId">
          <el-select v-model="form.teamId" placeholder="请选择团队外键">
            <el-option
              v-for="dict in teams"
              :key="dict.id"
              :label="dict.name"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="全年目标" prop="targets">
          <el-input v-model="form.targets" placeholder="请输入全年目标" />
        </el-form-item> -->
        <el-form-item label="1月" prop="january">
          <el-input v-model="form.january" placeholder="请输入1月" />
        </el-form-item>
        <el-form-item label="2月" prop="february">
          <el-input v-model="form.february" placeholder="请输入2月" />
        </el-form-item>
        <el-form-item label="3月" prop="march">
          <el-input v-model="form.march" placeholder="请输入3月" />
        </el-form-item>
        <el-form-item label="4月" prop="april">
          <el-input v-model="form.april" placeholder="请输入4月" />
        </el-form-item>
        <el-form-item label="5月" prop="may">
          <el-input v-model="form.may" placeholder="请输入5月" />
        </el-form-item>
        <el-form-item label="6月" prop="june">
          <el-input v-model="form.june" placeholder="请输入6月" />
        </el-form-item>
        <el-form-item label="7月" prop="july">
          <el-input v-model="form.july" placeholder="请输入7月" />
        </el-form-item>
        <el-form-item label="8月" prop="august">
          <el-input v-model="form.august" placeholder="请输入8月" />
        </el-form-item>
        <el-form-item label="9月" prop="september">
          <el-input v-model="form.september" placeholder="请输入9月" />
        </el-form-item>
        <el-form-item label="10月" prop="october">
          <el-input v-model="form.october" placeholder="请输入10月" />
        </el-form-item>
        <el-form-item label="11月" prop="november">
          <el-input v-model="form.november" placeholder="请输入11月" />
        </el-form-item>
        <el-form-item label="12月" prop="december">
          <el-input v-model="form.december" placeholder="请输入12月" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Teamper">
import { listTeamper, getTeamper, delTeamper, addTeamper, updateTeamper } from "@/api/teamper/teamper";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { listInstitution} from "@/api/system/institution";
import { listTeam} from "@/api/system/team";

const { proxy } = getCurrentInstance();
const { years } = proxy.useDict('years');

const teamperList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


// 定义一个institutions数组，用于存储机构数据
const institutions = ref([]);
// 定义一个方法、动态获取机构数据
function getInstitutions() {
  listInstitution().then(response => {
    institutions.value = response.rows;
    // console.log(institutions.value);
  });
}

// 定义一个teams数组，用于存储团队数据
const teams = ref([]);
// 定义一个方法、动态获取团队数据
function getTeams() {
  listTeam().then(response => {
    teams.value = response.rows;
    console.log(teams.value);
  });
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    year: null,
    institutionId: null,
    teamId: null,
    targets: null,
    january: null,
  },
  rules: {
    teamId: [
      { required: true, message: "团队外键不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// 根据月份计算季度金额
function calculateQuarterAmount(monthAmounts) {
  const quarter = {
    onequarter: 0,
    twoquarter: 0,
    threequarter: 0,
    fourquarter: 0,
    alltarget: 0
  };

  quarter.onequarter = parseInt(monthAmounts.january) + parseInt(monthAmounts.february) + parseInt(monthAmounts.march);
  quarter.twoquarter = parseInt(monthAmounts.april) + parseInt(monthAmounts.may) + parseInt(monthAmounts.june);
  quarter.threequarter = parseInt(monthAmounts.july) + parseInt(monthAmounts.august) + parseInt(monthAmounts.september);
  quarter.fourquarter = parseInt(monthAmounts.october) + parseInt(monthAmounts.november) + parseInt(monthAmounts.december);
  // console.log(quarter)
  quarter.alltarget = parseInt(quarter.onequarter) + parseInt(quarter.twoquarter) + parseInt(quarter.threequarter) + parseInt(quarter.fourquarter);
  return quarter;
}

/** 查询团队业绩目标列表 */
function getList() {
  loading.value = true;
  listTeamper(queryParams.value).then(response => {
    teamperList.value = response.rows;
    teamperList.value.forEach(row => {
      const quarter = calculateQuarterAmount(row);
      row.onequarter = quarter.onequarter;
      row.twoquarter = quarter.twoquarter;
      row.threequarter = quarter.threequarter;
      row.fourquarter = quarter.fourquarter;
      row.alltarget = quarter.alltarget;
    });
    console.log(response.rows)
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    year: null,
    institutionId: null,
    teamId: null,
    targets: null,
    january: null,
    february: null,
    march: null,
    april: null,
    may: null,
    june: null,
    july: null,
    august: null,
    september: null,
    october: null,
    november: null,
    december: null,
    onequarter: null,
  };
  proxy.resetForm("teamperRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加团队业绩目标";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTeamper(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改团队业绩目标";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["teamperRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTeamper(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTeamper(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除团队业绩目标编号为"' + _ids + '"的数据项？').then(function() {
    return delTeamper(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('teamper/teamper/export', {
    ...queryParams.value
  }, `teamper_${new Date().getTime()}.xlsx`)
}

getList();
// 加载机构数据
getInstitutions();
// 加载团队数据
getTeams();
</script>
